#用来做词云图
import jieba
from wordcloud import WordCloud
import os 
def cloud_path(name:str)->str:
    path = 'D:\\Graduation-Project\\serve\\static\\wordCloud\\'+ name + '.jpg'
    #如果已有，则直接返回
    if(not os.path.exists(path)):
        print(path)
        c_path = cloud(name)
        return c_path
    else:
        return '/static/wordCloud/'+ name + '.jpg'
def cloud(name)->str:
    comment_str = ''
    try:
        with open('D:\\Graduation-Project\\serve\\static\\comment\\'+ name + '.txt',"r",encoding='utf-8') as f:
            comment_str = f.read()
    except Exception as e:
        comment_str = ''
    if(comment_str):
        words = jieba.lcut(comment_str)     #精确分词
        newtxt = ''.join(words)    #空格拼接
        wordcloud = WordCloud(font_path =  "D:\\Graduation-Project\\serve\\analysisapp\\font\\钟齐志莽行书.ttf",background_color='white', height=600,width=600,scale=20).generate(newtxt)
        #存入静态目录
        path = 'D:\\Graduation-Project\\serve\\static\\wordCloud\\'+ name + '.jpg'
        wordcloud.to_file(path)
        return '/static/wordCloud/'+ name + '.jpg'
    else:
        return ''